<?php

namespace App\Admin\Controllers\cert;

use App\Enums\PetSex;
use App\Models\Cert;
use App\Models\CertProvince;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CertController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '证件管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Cert);
        $grid->expandFilter();
        $grid->column('cert_id', __('Cert id'))->sortable();
        $grid->column('cert_no', __('Cert no'));
        $grid->column('created_at', __('Created at'));
        $grid->column('pet_name', __('Pet name'));
        $grid->column('cert_province.min', __('省份'));
        $grid->column('birthday', __('Birthday'));
        $grid->column('modal', __('弹窗'))->modal(function ($grid){
            return 'hello';
        });

        $grid->column('sex', __('Sex'))->display(function ($sex) {
            $petsex = PetSex::getPetSex();
            if (!empty($petsex[$sex])) {
                return $petsex[$sex];
            } else {
                return '未选择';
            }
        });
        $grid->column('big_cat', __('Big cat'));
        $grid->column('cert_images', __('Cert images'))->gallery(['width' => 50, 'height' => 50]);
        $grid->column('cert_people_info', __('Cert people info'));
        $grid->column('vaccine_info', __('Vaccine info'));
        $grid->column('insect_info', __('Insect info'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Cert::findOrFail($id));

        $show->field('cert_id', __('Cert id'));
        $show->field('cert_no', __('Cert no'));
        $show->field('updated_at', __('Updated at'));
        $show->field('created_at', __('Created at'));
        $show->field('pet_name', __('Pet name'));
        $show->field('province', __('Province'));
        $show->field('birthday', __('Birthday'));
        $show->field('sex', __('Sex'));
        $show->field('big_cat', __('Big cat'));
        $show->field('cert_images', __('Cert images'));
        $show->field('cert_people_info', __('Cert people info'));
        $show->field('vaccine_info', __('Vaccine info'));
        $show->field('insect_info', __('Insect info'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Cert);
        $form->tab('基本信息',function (Form $form){
            $form->text('cert_no', __('Cert no'));
            $form->text('pet_name', __('Pet name'));
            $form->select('province', __('Province'))->options(
                CertProvince::pluck('min', 'cert_province_id')
            );
            $form->multipleImage('cert_images', __('Cert images'))->removable();
            $form->date('birthday', __('Birthday'))->default(date('Y-m-d'));
            $form->select('sex', __('Sex'))->options(PetSex::getPetSex());
            $form->text('big_cat', __('Big cat'));
            $form->text('cert_people_info', __('Cert people info'));
            $form->textarea('insect_info', __('Vaccine info'));
        })->tab('免疫信息',function (Form$form){
            $form->hasMany('vaccine',function (Form\NestedForm $form){
                $form->text('vaccine_name');
                $form->image('vaccine_img_process','免疫过程');
                $form->image('vaccine_img_bottle','免疫瓶');
            });
        });

        return $form;
    }
}
